package isp.apps.example.shoppingcart.persistence.hibernate;

import isp.apps.example.shoppingcart.domain.Item;

import isp.apps.example.shoppingcart.persistence.ItemDirectory;

import isp.apps.example.shoppingcart.persistence.PersistenceManagerFactory;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import isp.utils.cjis.persistence.PersistenceException;

import isp.utils.cjis.persistence.PersistenceManager;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class HibernateItemDirectory implements ItemDirectory
{

  private PersistenceManager persistenceManager =
    PersistenceManagerFactory.getPersistenceManager();

  private static final Log log =
    LogFactory.getLog(HibernateItemDirectory.class);

  public HibernateItemDirectory()
  {
  }

  /**
	 * @return
	 */
  public List getItems()
  {
    try
    {
      return persistenceManager.find("get.items", Collections.EMPTY_LIST);
    }
    catch (PersistenceException pe)
    {
      log.error(pe, pe);
      return null;
    }
  }

  public Item getItemBySku(String sku)
  {
    try
    {
      List params = new LinkedList();
      params.add(sku);
      List l = persistenceManager.find("get.itemBySku", params);
      if (l.isEmpty())
         return null;
      return (Item)l.get(0);
    }
    catch (PersistenceException pe)
    {
      log.error(pe, pe);
      return null;
    }
  }

  public Item getItem(Integer id)
  {
    try
    {
      return (Item) persistenceManager.load(Item.class, id);
    }
    catch (PersistenceException pe)
    {
      log.error(pe, pe);
      return null;
    }
  }

  public List getValidItems()
  {
    try
    {
      return persistenceManager.find("get.validItems", Collections.EMPTY_LIST);
    }
    catch (PersistenceException pe)
    {
      log.error(pe, pe);
      return null;
    }
  }

  public List getInvalidItems()
  {
    try
    {
      return persistenceManager.find("get.invalidItems", Collections.EMPTY_LIST);
    }
    catch (PersistenceException pe)
    {
      log.error(pe, pe);
      return null;
    }
  }

  public List getValidInStockItems()
  {
    try
    {
      return persistenceManager.find("get.validInStockItems", Collections.EMPTY_LIST);
    }
    catch (PersistenceException pe)
    {
      log.error(pe, pe);
      return null;
    }
  }

  public List getValidOutOfStock()
  {
    try
    {
      return persistenceManager.find("get.validOutOfStock", Collections.EMPTY_LIST);
    }
    catch (PersistenceException pe)
    {
      log.error(pe, pe);
      return null;
    }
  }

  public List getOutOfStockItems()
  {
    try
    {
      return persistenceManager.find("get.outOfStockItems", Collections.EMPTY_LIST);
    }
    catch (PersistenceException pe)
    {
      log.error(pe, pe);
      return null;
    }
  }

  public List getInStockItems()
  {
    try
    {
      return persistenceManager.find("get.inStockItems", Collections.EMPTY_LIST);
    }
    catch (PersistenceException pe)
    {
      log.error(pe, pe);
      return null;
    }
  }

  public List getItemsByInventoryCount(Integer count)
  {
    try
    {
      return persistenceManager.find("get.itemsByInventoryCount", Collections.EMPTY_LIST);
    }
    catch (PersistenceException pe)
    {
      log.error(pe, pe);
      return null;
    }
  }

  public List getItemsBelowInventoryCount(Integer count)
  {
    try
    {
      return persistenceManager.find("get.itemsBelowInventoryCount", Collections.EMPTY_LIST);
    }
    catch (PersistenceException pe)
    {
      log.error(pe, pe);
      return null;
    }
  }
}
